<?php

namespace XWAM\Controller;

/**
 * WebsiteMenu 控制器类
 *
 * @since 1.0 <2016-6-17> SoChishun <14507247@qq.com> Added.
 */
class WebsiteMenu extends AppbaseController {

    function indexSearch() {
        $search = I('param.');
        if (!empty($search['word'])) {
            $word = $search['word'];
            $where['_string'] = "title like '%$word%' or code like '%$word%'";
        }
        // $where['site_id'] = $this->site_id;
        if (!$where) {
            $where['pid'] = 0;
        }
        return array('search' => $search, 'where' => $where);
    }

    public function index() {
        $asearch = $this->indexSearch();
        $mMenu = new \XWAM\Model\WebsiteMenuModel();
        $list = $mMenu->selectTree(array('where' => $asearch['where']));
        $this->assign('tree', $list);
        $this->assign('search', $asearch['search']);
        $this->display();
    }

    public function edit($id = 0, $pid = 0) {
        $data = array();
        if ($id) {
            $mMenu = new \XWAM\Model\WebsiteMenuModel();
            $data = $mMenu->find($id);
        }
        if (!$data) {
            $data = array('user_name' => $this->userName, 'site_id' => $this->siteId);
        }
        if ($pid) {
            $data['pid'] = $pid;
        }
        $this->assign('data', $data);
        $this->display();
    }

    public function editSave() {
        $mMenu = new \XWAM\Model\WebsiteMenuModel();
        $result = $mMenu->saveMenu();
        $this->dialogJump($result['status'], $result['info']);
    }

    public function getTreeJson($pid = 0) {
        // $where['site_id'] = $this->site_id;
        $where['pid'] = $pid;
        $mMenu = new \XWAM\Model\WebsiteMenuModel();
        $data = $mMenu->selectTreeJson(array('where' => $where));
        $this->ajaxReturn($data);
    }

    function copy($id = 0) {
        if (!$id) {
            $this->ajaxMsg(false, '参数无效');
        }
        $mMenu = new \XWAM\Model\WebsiteMenuModel();
        $mMenu->copyMenu($id);
        $this->ajaxMsg(true);
    }

    function changeStatus($id, $status) {
        $mMenu = new \XWAM\Model\WebsiteMenuModel();
        $this->ajaxReturn($mMenu->changeStatus($id, $status));
    }

    function changeSort($id, $sort) {
        $mMenu = new \XWAM\Model\WebsiteMenuModel();
        $this->ajaxReturn($mMenu->changeSort($id, $sort));
    }

    function delete($id = '') {
        $mMenu = new \XWAM\Model\WebsiteMenuModel();
        $this->ajaxReturn($mMenu->deleteMenu($id));
    }

}
